const app = getApp();
const path = app.globalData.path;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noticeList: [{ "todoTitle": 'todoTitle' }, { "todoTitle": 'todoTitle' }],
    page: 1,
    start: 0,
    limit: 25,
    read: '',
    unRead: '',
    opened: false, 
    unopen: true, 
    checkBoxItem: 
    [
      { 
        code: '0', 
        value: '已读' 
      }, 
      { 
        code: '1', 
        value: '未读', 
        checked: 'true'
      }
    ] 
  }, 


  filterCheck: function(e) {  
    var that = this; 
   // console.log('checkbox发生change事件，携带value值为：', e.detail.value) 
    var selectedIndexList = e.detail.value; 
  //  console.log(selectedIndexList); 
    var len =  selectedIndexList.length; 
    if(len == 1){ 
      if(selectedIndexList[0] == 0){ 
        //console.log("已读"); 
        that.setData({ 
          opened: true, 
          unopen:false 
          }) 
      }else{ 
       // console.log("未读"); 
        that.setData({ 
          opened:false, 
          unopen: true 
        }) 
      } 
    }else if(len == 2){ 
     // console.log("全选"); 
        that.setData({ 
          opened: true, 
          unopen:true 
     
        }) 
    }else { 
     // console.log("全不选"); 
        that.setData({ 
          opened: false, 
          unopen: false 
           
        }) 
    } 

  // 每次改变条件重置一下 分页参数 
  that.setData({ 
    page:1, 
    start:0 
  }) 

  let loginFlag = wx.getStorageSync('token'); 

  if (loginFlag) { 
    wx.showLoading({ 
      title: '加载中....', 
    }) 
    wx.request({ 
      url: path + "wx-mw/notify-list!getNotifyList.action", 

      header: { 
        'token': loginFlag 
      }, 
      dataType: "JSON", 
      data: { 
        searchOpened: that.data.opened, 
        searchUnopen: that.data.unopen, 
        page: 1, 
        start: 0, 
        limit: 25 
      }, 
      success: function (res) { 
        wx.hideLoading(); 
        var result = JSON.parse(res.data); 
        that.setData({ 
          noticeList: result.data 
        }) 
      }, 
      error: function (res) { 
        console.log(res); 
      } 
    }) 

  } else { 
    that.data.readCheck = true; 
    that.data.unReadCheck == false; 
    // 无skey，作为首次登录 
    wx.navigateTo({ 
      url: '../../../login/login', 
    }) 
  } 
  that.onShow(); 
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    // that.onShow()
  },

  jumpToDo:function(e){
    // console.log(e);
    var that = this;
      var loginFlag = wx.getStorageSync('token');
    if (loginFlag) {

        // 点击通知先发送请求 "开封" ，成功返回后  再跳转到相应的处理页面
        wx.showLoading({
          
        })
      wx.request({
        url: path + "wx-mw/notify-list!openNotification.action",
        header: {
          'token': loginFlag
        },
        dataType: "JSON",
        data: {
          notifyId: e.currentTarget.dataset.id
        },
        success: function (res) {
          that.queryIssueInfoThenSkipTodo(e,loginFlag);
        },
        error: function (res) {
          console.log(res);
        }
      })


    } else {

      // 无skey，作为首次登录
      wx.navigateTo({
        url: '../../../login/login',
      })
    }
  },


  queryIssueInfoThenSkipTodo:function(e,loginFlag){
    wx.request({
      url: path + "wx-issue/q-iissue!getQimIssueInfoByReportNo.action",

      header: {
        'token': loginFlag
      },
      dataType: "JSON",
      data: {
        searchModel: e.currentTarget.dataset.sourceid
      },

      success: function (res) {
        wx.hideLoading();
        let result = JSON.parse(res.data);
        let data = result.data;
        // console.log(data);
        wx.navigateTo({
          url: '../issue/todo/todoform?issueId=' + data.ID
            + "&programVehicleId=" + data.PROGRAM_VEHICLE_ID
            + "&processStatus=" + data.PROCESS_STATUS
            + "&lifecycleStatus=" + data.LIFECYCLE_STATUS

        })
      },
      error: function (res) {
        console.log(res);
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    let loginFlag = wx.getStorageSync('token');
   
    if (loginFlag) {
      wx.showLoading({
        title: '加载中....',
      })
      wx.request({
        url: path + "wx-mw/notify-list!getNotifyList.action",
        
        header: {
          'token': loginFlag
        },
        dataType: "JSON",
        data: {
          searchOpened: that.data.opened, 
          searchUnopen: that.data.unopen,  
        
          page: 1,
          start: 0,
          limit: 25
        },
        success: function (res) {
          wx.hideLoading();
          var result = JSON.parse(res.data);
          that.setData({
            noticeList: result.data
          })
        },
        error: function (res) {
          console.log(res);
        }
      })

    } else {
      // 无skey，作为首次登录
      wx.navigateTo({
        url: '../../../login/login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var loginFlag = wx.getStorageSync('token');
    wx.showLoading({
      title: '玩命加载中',
    })

    that.setData({
      page: that.data.page + 1,
      start: that.data.start + 25,
    })

    wx.request({
      url: path + "wx-mw/notify-list!getNotifyList.action",
      header: {
        'token': loginFlag
      },
      dataType: "JSON",
      data: {
        searchOpened: that.data.opened, 
        searchUnopen: that.data.unopen, 
        page: that.data.page,
        start: that.data.start,
        limit: 25
      },
      
      success: function (res) {
        var notices = that.data.noticeList;
        var result = JSON.parse(res.data);
        
        // console.log(result);
        var len = result.data.length;
        if (len == 0) {
          
          wx.showToast({
            title: '没有通知了',
            duration: 1500,
            icon: 'none'
          })
          return;
        }

        for (var i = 0; i < len; i++) {
          notices.push(result.data[i]);
        }
        // 设置数据
        that.setData({
          noticeList: notices
        })
        // 隐藏加载框
        wx.hideLoading();

      },
      error: function (res) {
        console.log(res);
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})